/*
Name:system_logs
Author: Terry Dong
Description: data access layer
*/
using System;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Reflection;
using Gammar.Digger.SystemFramework;
using Gammar.Digger.Entity;
using Gammar.Digger.IDAL;
using Gammar.Digger.Data;
namespace Gammar.Digger.DALSqlServer
{
    public partial class system_logs : Isystem_logs
    {
        private DbSqlServer helper;

        #region constructors
        /// <summary>
        /// default constructor with the defaultConn
        /// </summary>
        public system_logs()
        {
            helper = new DbSqlServer(ConnectionString.SqlServer_GPriceCollect);
        }

        /// <summary>
        /// default constructor with the defaultConn
        /// </summary>
        public system_logs(string connString)
        {
            helper = new DbSqlServer(connString);
        }
        #endregion

        #region IDataBase<system_logs>

        public int Add(system_logsEntity entity)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("INSERT INTO system_logs(");
            strSql.Append("event_type,description,causeFrom,create_time,sys_id)");
            strSql.Append(" VALUES (");
            strSql.Append("@event_type,@description,@causeFrom,@create_time,@sys_id)");
            SqlParameter[] parameters = {
					new SqlParameter("@event_type",SqlDbType.Int),
					new SqlParameter("@description",SqlDbType.Text),
					new SqlParameter("@causeFrom",SqlDbType.BigInt),
					new SqlParameter("@create_time",SqlDbType.BigInt),
					new SqlParameter("@sys_id",SqlDbType.Int)
					};
            parameters[0].Value = entity.event_type;
            parameters[1].Value = entity.description;
            parameters[2].Value = entity.causeFrom;
            parameters[3].Value = entity.create_time;
            parameters[4].Value = entity.sys_id;

            return helper.ExecuteNonQuery(strSql.ToString(), parameters);
        }

        public int Update(system_logsEntity entity)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("UPDATE system_logs SET ");
            strSql.Append("event_type=@event_type,");
            strSql.Append("description=@description,");
            strSql.Append("causeFrom=@causeFrom,");
            strSql.Append("create_time=@create_time,");
            strSql.Append("sys_id=@sys_id");

            strSql.Append(" WHERE logs_id=@logs_id");
            SqlParameter[] parameters = {
					new SqlParameter("@logs_id",SqlDbType.Int),
					new SqlParameter("@event_type",SqlDbType.Int),
					new SqlParameter("@description",SqlDbType.Text),
					new SqlParameter("@causeFrom",SqlDbType.BigInt),
					new SqlParameter("@create_time",SqlDbType.BigInt),
					new SqlParameter("@sys_id",SqlDbType.Int)
					};
            parameters[0].Value = entity.logs_id;
            parameters[1].Value = entity.event_type;
            parameters[2].Value = entity.description;
            parameters[3].Value = entity.causeFrom;
            parameters[4].Value = entity.create_time;
            parameters[5].Value = entity.sys_id;

            return helper.ExecuteNonQuery(strSql.ToString(), parameters);
        }

        public int UpdateBylogs_id(system_logsEntity entity)
        {
            string where = string.Format("logs_id='{0}'", entity.logs_id);
            entity.logs_id = null;
            return Updatepartial(entity, where);
        }

        public int Delete(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("DELETE FROM system_logs ");
            if (!string.IsNullOrEmpty(strWhere))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            return helper.ExecuteNonQuery(strSql.ToString());
        }

        public system_logsEntity GetEntity(int primaryKeyId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from system_logs ");
            strSql.Append(" where logs_id=@primaryKeyId");
            SqlParameter[] parameters = {
					new SqlParameter("@primaryKeyId", SqlDbType.Int)};
            parameters[0].Value = primaryKeyId;
            SqlDataReader dr = helper.ExecuteReader(strSql.ToString(), parameters) as SqlDataReader;
            if (dr.HasRows)
            {
                dr.Read();
                system_logsEntity entity = new system_logsEntity();
                if (!Convert.IsDBNull(dr["logs_id"]))
                {
                    entity.logs_id = (int)dr["logs_id"];
                }
                if (!Convert.IsDBNull(dr["event_type"]))
                {
                    entity.event_type = (int)dr["event_type"];
                }
                entity.description = dr["description"].ToString();
                if (!Convert.IsDBNull(dr["causeFrom"]))
                {
                    entity.causeFrom = (long)dr["causeFrom"];
                }
                if (!Convert.IsDBNull(dr["create_time"]))
                {
                    entity.create_time = (long)dr["create_time"];
                }
                if (!Convert.IsDBNull(dr["sys_id"]))
                {
                    entity.sys_id = (int)dr["sys_id"];
                }
                dr.Close();
                dr.Dispose();
                return entity;
            }
            else
            {
                dr.Close();
                dr.Dispose();
                return null;
            }
        }

        public object GetCell(string cellName, string where)
        {
            string strSql = string.Empty;
            if (string.IsNullOrEmpty(where))
            {
                strSql = string.Format("SELECT {0} FROM system_logs", cellName);
            }
            else
            {
                strSql = string.Format("SELECT {0} FROM system_logs Where {1}", cellName, where);
            }
            return helper.ExecuteCell(strSql);
        }

        public DataTable GetTable(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT logs_id,event_type,description,causeFrom,create_time,sys_id");
            strSql.Append(" FROM system_logs ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" WHERE " + strWhere);
            }
            return helper.ExecuteAdapter(strSql.ToString());
        }

        public SortList<system_logsEntity> GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select logs_id,event_type,description,causeFrom,create_time,sys_id");
            strSql.Append(" FROM system_logs ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            SqlDataReader dr = helper.ExecuteReader(strSql.ToString()) as SqlDataReader;
            SortList<system_logsEntity> list = new SortList<system_logsEntity>();
            while (dr.Read())
            {
                system_logsEntity entity = new system_logsEntity();
                if (!Convert.IsDBNull(dr["logs_id"]))
                {
                    entity.logs_id = (int)dr["logs_id"];
                }
                if (!Convert.IsDBNull(dr["event_type"]))
                {
                    entity.event_type = (int)dr["event_type"];
                }
                entity.description = dr["description"].ToString();
                if (!Convert.IsDBNull(dr["causeFrom"]))
                {
                    entity.causeFrom = (long)dr["causeFrom"];
                }
                if (!Convert.IsDBNull(dr["create_time"]))
                {
                    entity.create_time = (long)dr["create_time"];
                }
                if (!Convert.IsDBNull(dr["sys_id"]))
                {
                    entity.sys_id = (int)dr["sys_id"];
                }
                list.Add(entity);
            }

            dr.Close();
            dr.Dispose();

            return list;
        }


        #region select,add,update the fields that you want to operate.

        public int AddPartial(system_logsEntity entity)
        {
            int reval = 0;
            string sql = string.Empty;
            SqlParameter[] param = DataUtility.BuildAdd(entity, "system_logs", ref sql);

            if (param != null)
            {
                reval = helper.ExecuteNonQuery(sql, param);
            }
            return reval;

        }

        public int Updatepartial(system_logsEntity entity, string where)
        {
            int reval = 0;
            string sql = string.Empty;
            SqlParameter[] param = DataUtility.BuildUpdate(entity, "system_logs", where, ref sql);

            if (param != null)
            {
                reval = helper.ExecuteNonQuery(sql, param);
            }
            return reval;
        }

        public List<system_logsEntity> GetPartial(string[] selectedFields, string where)
        {
            List<system_logsEntity> list = new List<system_logsEntity>();
            string sql = DataUtility.BuildSelect(selectedFields, "system_logs", where);
            if (!string.IsNullOrEmpty(sql))
            {
                DbDataReader reader = helper.ExecuteReader(sql);

                while (reader.Read())
                {
                    system_logsEntity model = new system_logsEntity();
                    foreach (string field in selectedFields)
                    {
                        PropertyInfo propInfo = model.GetType().GetProperty(field);
                        propInfo.SetValue(model, reader[field], null);
                    }

                    list.Add(model);
                }

                reader.Close();
            }
            return list;
        }
        #endregion


        #endregion
    }
}
